function setvalue (id,value)
{
    (document.getElementById(id)).value = value;
}

function validatelogin (form)
{
    document.getElementById("loginbutton").disabled=true;

    document.getElementById("c_usernamel").innerHTML="";
    document.getElementById("c_passwordl").innerHTML="";
	
    document.getElementById("c_usernamel").style.visibility ="visible";
    document.getElementById("c_passwordl").style.visibility ="visible";
	
    var cekUsername=/^(\w|\.){5,}$/;
    var cekPassword=/.{6,}/;
	
    var booly=true;
	
    if (!cekUsername.test(form.username.value))
    {
        document.getElementById("c_usernamel").innerHTML="username tidak valid";
        booly=false;
    }else
    {
        document.getElementById("c_usernamel").style.visibility="hidden";
    }
	
    if (!cekPassword.test(form.password.value))
    {
        document.getElementById("c_passwordl").innerHTML="password kurang dari 6 karakter";
        booly=false;
    }else
    {
        document.getElementById("c_passwordl").style.visibility="hidden";
    }
	
    if (booly)
    {
        document.getElementById("loginbutton").disabled=false;
    }else document.getElementById("loginbutton").disabled=true;
	
    return booly;
}


function validate (form)
{
    document.getElementById("regbutton").disabled="true";

    document.getElementById("c_nama").innerHTML="";
    document.getElementById("c_username").innerHTML="";
    document.getElementById("c_password").innerHTML="";
    document.getElementById("c_confirm").innerHTML="";
    document.getElementById("c_email").innerHTML="";
	
    document.getElementById("c_nama").style.visibility ="visible";
    document.getElementById("c_username").style.visibility ="visible";
    document.getElementById("c_password").style.visibility ="visible";
    document.getElementById("c_confirm").style.visibility ="visible";
    document.getElementById("c_email").style.visibility ="visible";
	
    var cekNama=/^[A-Za-z\s]{5,}$/;
    var cekUsername=/^(\w|\.){5,}$/;
    var cekPassword=/.{6,}/;
    var cekEmail=/^(\w|.)+@(\w|.)+\.(\w){2,3}$/;
    var cekFile=/\.(bmp|jpg|jpeg|png)$/i;
    var cekLokasi=/.{5,}/;
	
    var booly=true;
	
    if (!cekNama.test(form.fullname.value))
    {

        document.getElementById("c_nama").innerHTML="invalid fullname";
        booly=false;
    }else
    {
        document.getElementById("c_nama").style.visibility="hidden";
		
    }
	
    if (!cekUsername.test(form.username.value))
    {

        document.getElementById("c_username").innerHTML="invalid username";
        booly=false;
    }else
    {
        document.getElementById("c_username").style.visibility="hidden";
		
    }
	
    if (!cekPassword.test(form.password.value))
    {

        document.getElementById("c_password").innerHTML="less than 6 characters";
        booly=false;
    }else
    {
        document.getElementById("c_password").style.visibility="hidden";
		
    }
	
    if (!cekEmail.test(form.email.value))
    {
        document.getElementById("c_email").innerHTML="invalid email adress";
        booly=false;
    }else
    {
        document.getElementById("c_email").style.visibility="hidden";
		
    }
	
    if (form.confirm.value.length <6)
    {
        document.getElementById("c_confirm").innerHTML="less than 6 characters";
        booly=false;
    }else if (form.password.value != form.confirm.value)
    {
        document.getElementById("c_confirm").innerHTML="password not same";
        booly=false;
    }else
    {
        document.getElementById("c_confirm").style.visibility="hidden";
					
    }
	
    if (booly)
    {
        document.getElementById("regbutton").disabled=false;
    }else document.getElementById("regbutton").disabled=true;
	
    return booly;
}

//tanggal
var dtCh= "-";
var minYear=1000;
var maxYear=9999;

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i==4 || i==6 || i==9 || i==11) {
            this[i] = 30
            }
        if (i==2) {
            this[i] = 29
            }
    }
    return this
}

function isDate(dtStr){
	
	
    document.getElementById("c_lahir").style.visibility="visible";
    var daysInMonth = DaysArray(12)
    var pos1=dtStr.indexOf(dtCh)
    var pos2=dtStr.indexOf(dtCh,pos1+1)
    var strYear=dtStr.substring(0,pos1)
    var strMonth=dtStr.substring(pos1+1,pos2)
    var strDay=dtStr.substring(pos2+1)
    strYr=strYear
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }
    month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)
    if (pos1==-1 || pos2==-1){
        document.getElementById("c_lahir").innerHTML="Format tanggal salah";
        return false
    }
    if (strMonth.length<1 || month<1 || month>12){
        document.getElementById("c_lahir").innerHTML="Bulan tidak valid";
        return false
    }
    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        document.getElementById("c_lahir").innerHTML="Tanggal tidak valid";
        return false
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        document.getElementById("c_lahir").innerHTML="Tahun tidak valid";
        return false
    }
    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
        document.getElementById("c_lahir").innerHTML="Format tanggal salah";
        return false
    }
    document.getElementById("c_lahir").style.visibility="hidden";
}


//====================================================
//Javascript untuk popup login dan register
//====================================================

function toggle(div_id) {
    var el = document.getElementById(div_id);
    if ( el.style.display != 'block' )
    {
        el.style.display = 'block';
    }
    else {
        el.style.display = 'none';
    }
}

function blanket_size(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportheight = window.innerHeight;
    } else {
        viewportheight = document.documentElement.clientHeight;
    }
    if ((viewportheight > document.body.parentNode.scrollHeight) && (viewportheight > document.body.parentNode.clientHeight)) {
        blanket_height = viewportheight;
    } else {
        if (document.body.parentNode.clientHeight > document.body.parentNode.scrollHeight) {
            blanket_height = document.body.parentNode.clientHeight;
        } else {
            blanket_height = document.body.parentNode.scrollHeight;
        }
    }
    var blanket = document.getElementById('blanket');
    blanket.style.height = blanket_height + 'px';
}

function window_pos(popUpDivVar) {
    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerHeight;
    } else {
        viewportwidth = document.documentElement.clientHeight;
    }
    if ((viewportwidth > document.body.parentNode.scrollWidth) && (viewportwidth > document.body.parentNode.clientWidth)) {
        window_width = viewportwidth;
    } else {
        if (document.body.parentNode.clientWidth > document.body.parentNode.scrollWidth) {
            window_width = document.body.parentNode.clientWidth;
        } else {
            window_width = document.body.parentNode.scrollWidth;
        }
    }
}

function popup(windowname) {
    blanket_size(windowname);
    window_pos(windowname);
    toggle('blanket');
    toggle(windowname);
}